﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Darius.API.Application.Commands.Defective
{
    public class DefectiveSearchHandler : IRequestHandler<DefectiveSearchCommand, Result>
    {
        private readonly IBaseRepositorys<Defectives> defectivesRepository;

        public DefectiveSearchHandler(IBaseRepositorys<Defectives> defectivesRepository)
        {
            this.defectivesRepository = defectivesRepository;
        }
        /// <summary>
        /// 不良品分布
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(DefectiveSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CodeEnum.search};
            var list1 =  defectivesRepository.GetAll().ToList();
            var list= from a in defectivesRepository.GetAll()
                      group a by a into a
                      select new DefectiveDto
                      {  
                          DefectiveId=a.Key.DefectiveId,
                          DefectiveNo=a.Key.DefectiveNo,
                          DefectiveName=a.Key.DefectiveName,
                          CreateDate = a.Key.CreateDate.ToString("yyyy-MM-dd"),
                          Count1=a.Count()
                      };
            result.Count = list1.Count();
            result.Data = list.ToList();
            return result;

        }
    }
}
